package com.example.orderfood2.viewAllInformation;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.orderfood2.DatabaseManager.DatabaseManager;
import com.example.orderfood2.R;
import com.example.orderfood2.Repository.ClassInfoRepository;
import com.example.orderfood2.Repository.ManagerRepository;
import com.example.orderfood2.util.DBUtil;

public class EditClassActivity extends AppCompatActivity {
//    private DBUtil dbUtil;
    private ClassInfoRepository dbUtil;
    private int classId; // 保存当前编辑的班级ID
    private TextView etClassId;
    private EditText etClassNumber, etClassName;
    private Button btnSubmit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        dbUtil= DatabaseManager.getInstance(this).getClassInfoRepository();
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_edit_class);
        // 初始化数据库
//        dbUtil = new DBUtil(this);
        // 获取传递过来的ID
        classId = getIntent().getIntExtra("id", -1);
        if (classId == -1) {
            Toast.makeText(this, "无效的班级ID", Toast.LENGTH_SHORT).show();
            finish();
            return;
        }
        // 初始化视图,获取各个组件
        initViews();
        // 加载班级数据
        loadClassData();
        // 设置提交按钮点击事件
        btnSubmit.setOnClickListener(new View.OnClickListener() {
            //为防止多个ViewAllClassInformationActivity重叠，
            //通过设置 Intent 的标志位 FLAG_ACTIVITY_CLEAR_TOP 和 FLAG_ACTIVITY_SINGLE_TOP 来解决这个问题。
            // FLAG_ACTIVITY_CLEAR_TOP 会清除目标活动之上的所有活动，FLAG_ACTIVITY_SINGLE_TOP 则保证如果目标活动已经在栈顶，不会重新创建它
            @Override
            public void onClick(View view) {
                updateClassInfo();
                Intent intent=new Intent(EditClassActivity.this,ViewAllClassInformationActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                startActivity(intent);
                finish(); // 关闭当前 EditClassActivity
            }
        });
    }

    private void initViews() {
        etClassId = findViewById(R.id.et_class_id);
        etClassNumber = findViewById(R.id.et_class_number);
        etClassName = findViewById(R.id.et_class_name);
        btnSubmit = findViewById(R.id.class_add);
    }

    private void loadClassData() {
        Cursor cursor = dbUtil.getClassById(classId);
        if (cursor != null && cursor.moveToFirst()) {
            // 填充数据到输入框
            etClassId.setText(String.valueOf(classId));
            etClassNumber.setText(cursor.getString(cursor.getColumnIndexOrThrow("classnumber")));
            etClassName.setText(cursor.getString(cursor.getColumnIndexOrThrow("classname")));
            cursor.close();
        } else {
            Toast.makeText(this, "班级信息加载失败", Toast.LENGTH_SHORT).show();
            finish();
        }
    }

    private void updateClassInfo() {
        String newNumber = etClassNumber.getText().toString().trim();
        String newName = etClassName.getText().toString().trim();

        if (newNumber.isEmpty() || newName.isEmpty()) {
            Toast.makeText(this, "请填写完整信息", Toast.LENGTH_SHORT).show();
            return;
        }
        // 更新数据库
        boolean success = dbUtil.updateClass(classId, newNumber, newName);
        if (success) {
            Toast.makeText(this, "修改成功", Toast.LENGTH_SHORT).show();
            setResult(RESULT_OK);
            finish();
        } else {
            Toast.makeText(this, "修改失败", Toast.LENGTH_SHORT).show();
        }
    }
    @Override
    protected void onDestroy() {
        if (dbUtil != null) {
            dbUtil.close();
        }
        super.onDestroy();
    }
}